<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item,index) in list" :key="index" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from "@/api/search";

export default {
  name: "SearchResult",
  components: {},
  props: {
    // 搜索父组件的内容
    searchText: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      pagesize: 10, // 条数
      pagenum: 1 // 页码数
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onLoad() {
      try {
        //  1.请求数据
        const { data: res } = await getSearchResult({
          page: this.pagenum,
          per_page: this.pagesize,
          q: this.searchText
        });
        console.log(res);
        //  2.追加列表
        this.list.push(...res.data.results);
        //  3.关闭弹框
        this.loading = false;
        //  4.判断所有是否完成
        if (res.data.results.length < this.pagesize) {
          this.finished = true;
        } else {
          this.pagenum++;
        }
      } catch (error) {
        this.$toast.fail('查询失败')
      }
    }
  }
};
</script>

<style scoped lang="less"></style>
